package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.Question;
import cn.wolfcode.trip.base.domain.Strategy;
import cn.wolfcode.trip.base.query.QuestionQueryObject;
import cn.wolfcode.trip.base.service.IQuestionService;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("question")
public class QuestionController {
    @Autowired
     private IQuestionService questionService;

    /**
     * 根据用户id查询问题数据
     * @param qo
     * @return
     */
    @GetMapping("{userId}")
    public PageInfo getStrategyById(QuestionQueryObject qo){
        //设置问题倒序查询
        qo.setOrderBy("q.createTime desc");
        qo.setPageSize(0);
        return questionService.queryByUserId(qo);
    }

    /**
     * 根据问题id查询问题数据
     * @param
     * @return
     */
    @GetMapping("/single/{questionId}")
    public Question getQuestionById(@PathVariable("questionId") Long questionId){
        Question question = questionService.selectById(questionId);
        return question;
    }

    /**
     * 查询已回答问题数据
     * @param
     * @return
     */
    @GetMapping("/answer")
    public PageInfo<Question> getQuestions(QuestionQueryObject qo){
        //创建时间降序
        qo.setOrderBy("q.answerSum desc,q.createTime desc");
        //设置分页长度
        qo.setPageSize(7);
        PageInfo<Question> pageInfo = questionService.queryHasAnswer(qo);
        return pageInfo;
    }
    /**
     * 查询没回答问题数据
     * @param
     * @return
     */
    @GetMapping("/noAnswer")
    public PageInfo<Question> getNoAnswerQuestions(QuestionQueryObject qo){
        //创建时间降序
        qo.setOrderBy("q.createTime desc");
        //设置分页长度
        qo.setPageSize(7);
        PageInfo<Question> pageInfo = questionService.queryNoAnswer(qo);
        return pageInfo;
    }

    @PostMapping("/save")
    public JSONResult saveQuestion(Question question){
        //新增
        questionService.save(question);
        return new JSONResult();
    }

    @GetMapping("/keyword")
    public PageInfo<Question> getSearchQuestions(QuestionQueryObject qo){
        //创建时间降序
        qo.setOrderBy("q.createTime desc");
        //设置分页长度
        qo.setPageSize(7);
        PageInfo<Question> pageInfo = questionService.queryKeyword(qo);
        return pageInfo;
    }

}
